package com.dingxin.ship_together_api.config.rabbitmq;

import com.dingxin.ship_together_api.service.OrderInfoService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;

import static com.dingxin.ship_together_api.config.rabbitmq.RabbitMQConfig.DEAD_LETTER_QUEUE_NAME;

@Slf4j
@Component
public class DeadLetterQueueConsumer {
    @Autowired
    private OrderInfoService orderInfoService;


    @RabbitListener(queues = DEAD_LETTER_QUEUE_NAME)
    public void receiveA(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody());
        log.info("{} 收到订单消息 {}", LocalDateTime.now() , msg);
        // 取消订单
        orderInfoService.cancelOrder(Long.valueOf(msg));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }


}

